home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / copyfiletofile.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  88 lines

  1. #include "kiss.h"
  2.  
  3. int copyfiletofile (char *src, char *dest, CpFlags fl)
  4. {
  5.     FILE
  6.     *inf,
  7.     *outf;
  8.     struct stat
  9.     inbuf,
  10.     outbuf;
  11.     char
  12.     destlink [FILENAMELEN];
  13.     register int
  14.     warnlevel = 0,
  15.     nholes;
  16.     struct utimbuf
  17.     tm;
  18.     
  19.     if (fl.verbose)
  20.     printf ("%s -> %s\n", src, dest);
  21.  
  22.     /* a. sourcefile must exist */
  23.     if (stat (src, &inbuf))
  24.     return (warning ("cannot stat input file \"%s\"", src));
  25.  
  26.     /* b. may destfile be overwritten? */
  27.     if (! stat (dest, &outbuf))
  28.     {
  29.     if (inbuf.st_ino == outbuf.st_ino)
  30.         return (warning ("\"%s\" and \"%s\" are identical files",
  31.                  src, dest));
  32.     if (fl.interactive)
  33.     {
  34.         printf ("%s: overwrite \"%s\" [y/?] ", progname, dest);
  35.         if (getinput (stdin) != 'y')
  36.         return (0);
  37.     }
  38.     }
  39.  
  40.     /* c. copying links? */
  41.     if (fl.noderef && islink (src, destlink))
  42.     {
  43.     if (symlink (destlink, dest))
  44.         return (warning ("failure creating symlink \"%s\" to \"%s\"",
  45.                  destlink, dest));
  46.     return (0);
  47.     }
  48.  
  49.     /* d. simple file to file */
  50.     if (! S_ISREG (inbuf.st_mode))
  51.     return (warning ("\"%s\" -> \"%s\": cannot copy non-regular "
  52.              "files (yet)", src, dest));
  53.  
  54.     if (! (inf = fopen (src, "r")) )
  55.     return (warning ("cannot open \"%s\" for reading", src));
  56.     if (! (outf = fopen (dest, "w")) )
  57.     return (warning ("cannot open \"%s\" for writing", dest));
  58.  
  59.     nholes = file2file (inf, outf);
  60.  
  61.     if (fl.verbose && nholes)
  62.     printf ("%d hole(s) bigger than %d\n", nholes, HOLESIZE);
  63.  
  64.     fclose (inf);
  65.     
  66.     if (fchmod (fileno (outf), inbuf.st_mode))
  67.     warnlevel += warning ("failure changing mode of \"%s\"", dest);
  68.  
  69.     fclose (outf);
  70.  
  71.     if (fl.preserve)
  72.     {
  73.     
  74.     if (chown (dest, inbuf.st_uid, inbuf.st_gid))
  75.         warnlevel += warning ("failure changing ownership of \"%s\"",
  76.                   dest);
  77.     
  78.     tm.actime = inbuf.st_atime;
  79.     tm.modtime = inbuf.st_mtime;
  80.     
  81.     if (utime (dest, &tm))
  82.         warnlevel += warning ("failure changing timestamps of \"%s\"",
  83.                   dest);
  84.     }
  85.  
  86.     return (warnlevel);
  87. }
  88.